package com.jcclub.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncryptUtil {
    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);  // 生成一对512位的 RSA 公钥私钥
            privateKey = keyPair[0];  // 私钥
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];  // 公钥
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    public static String encrypt(String planText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, planText);
        System.out.println("encrypt:" + encrypt);  // 私钥加密后
        return encrypt;
    }
    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        System.out.println("decrypt:" + decrypt);  // 公钥解密后
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("zhl2005606");

    }






}
